package com.qiaotongtianxia.loans.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.net.Api;
import com.qiaotongtianxia.loans.util.StatusBarUtils;

import java.util.List;

import butterknife.BindView;
import uk.co.senab.photoview.PhotoViewAttacher;


/**
 * Created by Administrator on 2017/5/23.
 * lisen
 * <p>
 * 查看多张图片的页面
 * <p/>
 */

public class PhotosActivity extends BaseActivity {
    public static final String IMAGES = "images";
    public static final String CURRENTPOSITION = "currentPosition";
    @BindView(R.id.viewPager) ViewPager viewPager;

    private List<String> images;

    private SparseArray<View> imageViews;

    private LayoutInflater inflater;

    @Override
    public int getLayoutId() {
        return R.layout.activity_photos;
    }

    @Override
    public void onBaseCreate(@Nullable Bundle savedInstanceState) {
        inflater = LayoutInflater.from(this);
        images = getIntent().getStringArrayListExtra(IMAGES);
        int currentPosition = getIntent().getIntExtra(CURRENTPOSITION,0);

        if (images != null && images.size() >0){
            imageViews= new SparseArray<>(images.size());
        }
        viewPager.setAdapter(new Adapter());
        viewPager.setCurrentItem(currentPosition);
    }

    @Override
    public void setStatusBarColor() {
        StatusBarUtils.setWindowStatusBarTrans(this);
    }

    @Override
    public Api onApiCreate() {
        return null;
    }

    private class Adapter extends PagerAdapter{

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view== object;
        }
        @Override
        public Object instantiateItem(final ViewGroup container, int position) {
            View view = imageViews.get(position);
            if (view == null){
                view = inflater.inflate(R.layout.item_activity_photos,container,false);
                final ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
                final ImageView photoView = (ImageView) view.findViewById(R.id.photoView);
                final PhotoViewAttacher attacher = new PhotoViewAttacher(photoView);
                attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                    @Override
                    public void onPhotoTap(View view, float x, float y) {
                        finish();
                    }
                });
                progressBar.setVisibility(View.VISIBLE);
                Glide.with(PhotosActivity.this).load(images.get(position)).into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        photoView.setImageDrawable(resource);
                        attacher.update();
                        progressBar.setVisibility(View.GONE);
                    }
                });
                imageViews.put(position,view);
            }
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            container.removeView(view);
        }
    }
}
